//
//  BBLoginRequest.m
//  BabyThermometer
//
//  Created by CaiLei on 10/21/14.
//  Copyright (c) 2014 baohu. All rights reserved.
//

#import "BBLoginRequest.h"
@implementation BBLoginRequestItem_babyInfo
@end

@implementation BBLoginRequestItem
@end

@implementation BBLoginRequest {
    void(^_completeBlock)(BBLoginRequestItem *retItem, NSError *error);
}

- (id)init {
    self = [super init];
    if (self) {
        self.urlHead = @"http://123.57.38.59:7001/baohu/servlet/login";
    }
    return self;
}

- (void)startRequestWithBlock:(void(^)(BBLoginRequestItem *retItem, NSError *error))aBlock {
    _completeBlock = aBlock;
    [self startRequest];
}

- (void)dealWithResponseJson:(NSString *)aJson andError:(NSError *)aError {
    if (aError) {
        _completeBlock(nil, aError);
        return;
    }
    
    BBLoginRequestItem *item = [[BBLoginRequestItem alloc] initWithString:aJson error:&aError];
    if (aError) {
        _completeBlock(nil, aError);
        return;
    }
    
    if (item.code.intValue == 1101) {
        aError = [NSError errorWithDomain:@"登录" code:1101 userInfo:@{@"msg": @"用户名密码错误"}];
        _completeBlock(item, aError);
        return;
    }
    
    if (item.code.intValue == 1102) {
        aError = [NSError errorWithDomain:@"登录" code:1102 userInfo:@{@"msg": @"用户不存在"}];
        _completeBlock(item, aError);
        return;
    }
    
    _completeBlock(item, nil);
}

@end
